#!/bin/bash

echo "检查active namenode"
active_json=`curl  http://10.33.50.201:50070/jmx?qry=Hadoop:service=NameNode,name=NameNodeStatus`
active_state=`echo $active_json|awk -F ',' '{print $(11)}'|awk -F ":" '{print $2}'|sed  's/"//g'|sed 's/}//g'|sed 's/]//g'|sed 's/ //g'`
if [ ${active_state} =  active ]; then
active_namenode=hdfs://10.33.50.201:8020
else
active_namenode=hdfs://10.33.50.202:8020
fi
echo ${active_namenode}

dt='{{ execution_date | cst_ds }}'
defaultFS=${active_namenode}
#defaultFS="hdfs://10.33.50.105:8020"

hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-1) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-2) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-3) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-4) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-5) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-6) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-7) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-8) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-9) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-10) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-11) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-12) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-13) | cst_ds }}
hdfs dfs -rm -r -f ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-14) | cst_ds }}


hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-1) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-1) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-2) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-2) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-3) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-3) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-4) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-4) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-5) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-5) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-6) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-6) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-7) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-7) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-8) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-8) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-9) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-9) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-10) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-10) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-11) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-11) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-12) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-12) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-13) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-13) | cst_ds }}
hadoop distcp  -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-14) | cst_ds }}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-14) | cst_ds }}
#if [ $? = 0 ];then
#  echo "hadoop distcp -overwrite -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/last_15_days ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt successfuly!"
#else
#  defaultFS2="hdfs://10.33.50.104:8020"
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt=${dt}  ${defaultFS}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt=${dt}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-1) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-1) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-2) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-2) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-3) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-3) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-4) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-4) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-5) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-5) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-6) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-6) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-7) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-7) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-8) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-8) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-9) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-9) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-10) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-10) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-11) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-11) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-12) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-12) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-13) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-13) | cst_ds }}
#hadoop distcp -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-14) | cst_ds }}  ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/dt={{ execution_date | date_add(-14) | cst_ds }}
#  if [ $? = 0 ];then
#    echo "hadoop distcp -overwrite -m 10 /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/last_15_days ${defaultFS2}/dw/hive/jms_dwd.db/external/dwd_scan_detail_dt successfully!"
#  else
#    echo "hadoop distcp -overwrite /dw/hive/jms_dwd.db/external/dwd_scan_detail_dt/last_15_days failed!"
#    exit 1
#  fi
#fi